declare namespace my = "urn:tva:metadata:2010";
declare namespace mpeg7 = "urn:tva:mpeg7:2008";

(:
declare variable $xmlfile := "file:///metadata/Redbee/uk/20130903181359samsung_.xml";
:)
(:
declare variable $xmlfile := "20130903181359samsung_.xml";
:)
declare variable $xmlfile := "20131006220011samsung.xml";
(:
  for $program in collection("redbee4.dbxml")//x:ProgramInformation
  where starts-with($program/x:BasicDescription/x:Title[contains(@type, "episodeTitle")], "The Social")
  return $program
:)
(:
  for $program in collection("redbee4.dbxml")//x:ProgramInformation
  for $title in $program/x:BasicDescription/x:Title
  where starts-with($title, "The Social")
  return $title
:)
(:
  for $program in collection("redbee4.dbxml")//x:ProgramInformation
  where some $title in $program/x:BasicDescription/x:Title satisfies (starts-with($title, "The Social"))
  return $program
:)
(:
  for $program in collection("redbee4.dbxml")//x:ProgramInformation
  where some $title in $program/x:BasicDescription/x:Title satisfies (matches($title, "^The Social"))
  return $program
:)
(: matches ("612-112-3321", "^612-\\d{3}-\\d{4}$") :)
(: matches ("612.112.3321", "^612[-\\.]\\d{3}[-\\.]\\d{4}$") :)

(:
let $areacode := "612"
let $match := concat("^", $areacode, "[-\\.]\\d{3}[-\\.]\\d{4}$")
return matches("612.423.1124", $match)
:)

(:
for $document in collection("redbee4.dbxml")/*
where $document[contains(dbxml:metadata("dbxml:name"),  "uk")]
return $document
:)

(:
for $schedule_source in collection("redbeeNew.dbxml")[dbxml:metadata("dbxml:name")="uk-20130903"]//x:ServiceInformation
for $lineup_source in collection("redbeeNew.dbxml")[dbxml:metadata("dbxml:name")="UK-Freesat-121-9-20130902"]//x:ServiceCollection/x:Services/x:Service
:)

(:
for $schedule_source in doc("redbeeSchedule.dbxml/uk-20130903")//x:ServiceInformation
for $lineup_source in doc("redbeeLineup.dbxml/UK-Freesat-121-9-20130902")//x:ServiceCollection[x:ServiceOf/@crid="srid://bds.tv/12126" and x:ServiceOf/@Device_Type_code="X"]/x:Services/x:Service
where matches($schedule_source/@serviceId, concat("^", $lineup_source/@id, "$"))
return ("Name: ", string($lineup_source/x:Name), " |id: ", string($lineup_source/@id))
:)

(:
for $lineup_source in doc("redbeeNew.dbxml/UK-Freesat-121-9-20130902")//x:ServiceCollection[x:ServiceOf/@crid="srid://bds.tv/12126" and x:ServiceOf/@Device_Type_code="X"]/x:Services/x:Service
for $schedule_source in doc("redbeeNew.dbxml/uk-20130903")//x:ServiceInformation
where matches($schedule_source/@serviceId, concat("^", $lineup_source/@id, "$"))
return ("Name: ", string($lineup_source/x:Name), " |id: ", string($lineup_source/@id))
:)

(:
for $lineup_source in collection("redbeeLineup.dbxml")//x:ServiceCollection/x:Services/x:Service
where matches($lineup_source/@id, "759895")
return ("Name: ", string($lineup_source/x:Name), " |id: ", string($lineup_source/@id))
:)

(:
declare namespace my = "http://brians.org/temperature";
declare function my:binary ($dec as xs:decimal) {
  if ($dec eq 0 or $dec eq 1)
    then $dec
  else
    let $m := xs:integer($dec div 2)
    let $j := $dec mod 2
    let $D := my:binary($m)
    return concat(string($D), string($j))
};
my:binary(46)
:)

declare function my:epigroups ($program) {
  let $programid := $program/@programId/string()
  let $epigroupid := $program/my:EpisodeOf/@crid/string()
  return
    if (empty($epigroupid))
      then concat("|S| ", $programid, " | ", string($program/my:BasicDescription/my:Title[@type="main"][1]), " |ET| ", string($program/my:BasicDescription/my:Title[@type="episodeTitle"][1]))
    else
      let $Uppergroup := doc($xmlfile)//my:GroupInformation[@groupId = $epigroupid]
      return concat("|G| ", $epigroupid, " | ", string($Uppergroup/my:BasicDescription/my:Title[1]), " | ", string($Uppergroup/my:BasicDescription/my:Title[1]/@type), " |S| ", $programid, " | ", string($program/my:BasicDescription/my:Title[@type="main"][1]), " |ET| ", string($program/my:BasicDescription/my:Title[@type="episodeTitle"][1]))
};

declare function my:check_program_memberof ($program) {
  let $programid := $program/@programId/string()
  let $groupinfo := doc($xmlfile)//my:GroupInformation
  let $mofs := for $memberof in $program/my:MemberOf
                  where $groupinfo[@groupId = $memberof/@crid/string()]/my:GroupType/@value/string() != "programConcept"
              return $groupinfo[@groupId = $memberof/@crid/string()]
  let $nummofs := count($mofs)
  for $mof at $count in $mofs 
  return 
    if ($count = 1 and $count = $nummofs)
      then concat($programid, " | ", $mof/@groupId, " (", $mof/my:GroupType/@value, ")* ")
    else if ($count = 1 and $count < $nummofs)
      then concat($programid, " | ", $mof/@groupId, " (", $mof/my:GroupType/@value, ") ")
    else if ($count > 1 and $count = $nummofs)
      then concat($mof/@groupId, " (", $mof/my:GroupType/@value, ")*")
    else concat($mof/@groupId, " (", $mof/my:GroupType/@value, ") ")
};
declare function my:get_group_memberof ($crid) {
  let $groupinfo := doc($xmlfile)//my:GroupInformation
  let $c_groupinfo := $groupinfo[@groupId = $crid]
  let $grouptitle := $c_groupinfo/my:BasicDescription/my:Title[1]
  let $groupid := $c_groupinfo/@groupId/string()
  let $imageurl := $c_groupinfo/my:BasicDescription/my:RelatedMaterial/my:MediaLocator/mpeg7:MediaUri
  let $imageexist := exists($imageurl)
  let $mofs := for $memberof in $c_groupinfo/my:MemberOf
                where $groupinfo[@groupId = $memberof/@crid/string()]/my:GroupType/@value/string() != "programConcept"
                return $memberof/@crid/string()
  let $nummofs := count($mofs)
  return
    if ($nummofs = 0)
      then concat($groupid, "(", $c_groupinfo/my:GroupType/@value, ")[", $grouptitle, "]<", $imageexist, ">*")
    else
      for $mof at $count in $mofs
      let $next := my:get_group_memberof($mof)
      return concat($groupid, "(", $c_groupinfo/my:GroupType/@value, ")[", $grouptitle, "]<", $imageexist, "> => ", $next)
};

declare function my:check_new_program_memberof ($program) {
  let $programid := $program/@programId/string()
  let $groupinfo := doc($xmlfile)//my:GroupInformation
  let $mofs := for $memberof in ($program/my:MemberOf, $program/my:EpisodeOf)
                  where $groupinfo[@groupId = $memberof/@crid/string()]/my:GroupType/@value/string() != "programConcept"
              return $groupinfo[@groupId = $memberof/@crid/string()]
  let $nummofs := count($mofs)
  let $imageurl := $program/my:BasicDescription/my:RelatedMaterial/my:MediaLocator/mpeg7:MediaUri
  let $programtitle := $program/my:BasicDescription/my:Title[@type="main"][1]
  let $imageexist := exists($imageurl)

  return
  if ($nummofs = 0)
    then concat($programid, " [", $programtitle, "]<", $imageexist, ">*")
  else
    for $mof at $count in $mofs 
    return 
      let $next := my:get_group_memberof($mof/@groupId/string())
      return concat($programid, " [", $programtitle, "]<",$imageexist, "> => ", $next)
};

(:
for $program in doc("redbeeSchedule.dbxml/uk-20130903")//my:ProgramInformation
  return my:epigroups($program)
:)


for $program in doc($xmlfile)//my:ProgramInformation
  return my:check_new_program_memberof($program)

(:
my:get_group_memberof("crid://bds.tv/21467489")
:)

(:
for $groupinfo in doc($xmlfile)//my:GroupInformation
let $groupid := $groupinfo/@groupId/string()
where $groupinfo/my:GroupType/@value/string() != "programConcept"
  return my:get_group_memberof($groupid)
:)

(:
my:get_group_memberof("crid://bds.tv/10541510")
:)
